<?php
// +----------------------------------------------------------------------
// | 海豚PHP框架 [ DolphinPHP ]
// +----------------------------------------------------------------------
// | 版权所有 2016~2017 河源市卓锐科技有限公司 [ http://www.zrthink.com ]
// +----------------------------------------------------------------------
// | 官方网站: http://dolphinphp.com
// +----------------------------------------------------------------------
// | 开源协议 ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------

namespace app\shop\admin;

use app\admin\controller\Admin;
use app\common\builder\ZBuilder;
use app\shop\model\ShopStatus as ShopStatusModel;
use think\Db;

/**             
 * 行为管理控制器
 * @package app\admin\controller
 */
class Shopcategory extends Admin
{
    public function index()
    {
        cookie('__forward__', $_SERVER['REQUEST_URI']);
        $status=ShopStatusModel::getStatus();
        $map = $this->getMap();
        $shop_status = input('shop_status');
        $search=input('search');
        if($shop_status=='0' || !empty($shop_status) || !empty($search)){
            if($shop_status!='0'){
                $map['status'] = $shop_status;
            }
            if($shop_status=='0'){
                $map['status']='0';
            }
            if($search){
                $map['typename']=['like',"%$search%"];
            }
        }
        $data_list = ShopStatusModel::getShopName($map);

        // 分页数据
        $page = $data_list->render();
        // 使用ZBuilder快速创建数据表格
        return ZBuilder::make('mix')
            ->setPageTitle('筛选')
            ->addSelect('shop_status', '审核状态', '', $status, $shop_status)
            ->addText('search','类目名称','',$search)
            ->addBtn('<button type="submit" class="btn  btn-primary btn-rounded">确定搜索</button>')
            ->hideBtn('submit,back')
            ->layout(['shop_status'=>3,'search'=>3])
            ->setPageTitle('经营类目审核')// 设置页面标题
            ->setTableName('type_auditing')// 设置数据表名
            ->addColumns([ // 批量添加列
                ['name', '店铺名称',''],
                ['typename', '类目名称','','','','fix-table-td'],
                ['state', '审核状态','status','',$status],
                ['right_button', '操作', 'btn']
            ])
            ->addFilter('status',$status)
            ->addTopButton('enable',['title'=>'审核'])
            ->addTopButton('delete')
            ->addRightButtons(['edit'=>['title'=>'通过','icon'=>'fa fa-fw fa-toggle-on','href'=>url('pass',['id' => '__id__'])]])
            ->addRightButton('delete',['href'=>url('del',['id' => '__id__'])])
            ->setRowList($data_list)// 设置表格数据
            ->isAjax(false)
            ->setFormMethod('get')
            ->setPages($page)// 设置分页
            ->fetch(); // 渲染页面
    }

    //批量审核启用
    public function enable($record = [])
    {
        $ids=input('');
        $ids=implode(',',$ids['ids']);
        $data=array('status'=>1);
        $details = "所属模块('shop'),操作日志('审核启用经营类目'),经营类目ID('$ids')";
        action_log('shoptype_enable', 'shop', $ids, UID, $details);
        $this->auditing($data);

    }

    public function auditing($data){
        $ids= $this->request->isPost() ? input('post.ids/a') : input('param.ids');
        foreach ($ids as $values){
            $res=Db::name("type_auditing")->where('id',$values)->update($data);
        }
        if ($res) {
            return $this->success('操作成功',cookie('__forward__'));
        } else {
            return $this->error('操作失败');
        }
    }
    //审核通过
    public function pass($id=''){
        $status['status']=1;
        $res=Db::name("type_auditing")->where('id',$id)->update($status);
        if ($res) {
            return $this->success('操作成功',cookie('__forward__'));
        } else {
            return $this->error('操作失败');
        }
    }
    //删除
    public function del($id=''){
        $res=Db::name("type_auditing")->where('id',$id)->delete();
        if ($res) {
            return $this->success('操作成功',cookie('__forward__'));
        } else {
            return $this->error('操作失败');
        }
    }
    //批量删除
    public function delete($record = [])
    {
        $ids        = $this->request->isPost() ? input('post.ids/a') : input('param.ids');
        foreach ($ids as $values){
            $res=Db::name("type_auditing")->where('id',$values)->delete();
        }
        if ($res) {
            return $this->success('删除成功',cookie('__forward__'));
        } else {
            return $this->error('删除失败');
        }
    }
}